<?php defined('SYSPATH') or die('No direct script access.');

/**
 * 数学公式,数字相关处理类
 */
class St_Math{


    /**
     * @function 生成随机数
     * @param int $length
     * @return int
     */
    public static function get_random_number($length = 4)
    {
        $min = pow(10, ($length - 1));
        $max = pow(10, $length) - 1;
        return mt_rand($min, $max);
    }

    /**
     * 加
     * @param array $numbers
     * @param int $scale
     * @return string
     */
    public static function add(array $numbers, int $scale): string
    {
        $res = '0';
        foreach ($numbers as $number) {
            $res = bcadd($res, (string)$number, $scale);
        }
        return $res;
    }

    /**
     * 减
     * @param array $numbers
     * @param int $scale
     * @return string
     */
    public static function sub(array $numbers, int $scale): string
    {
        $res = sprintf("%.2f",$numbers[0]);
        unset($numbers[0]);
        foreach ($numbers as $number) {
            $res = bcsub($res, (string)$number, $scale);
        }
        return $res;
    }

    /**
     * 除
     * @param array $numbers
     * @param int $scale
     * @return string
     */
    public static function div(array $numbers, int $scale = 0): string
    {
        $res = sprintf("%.2f",$numbers[0]);
        unset($numbers[0]);
        foreach ($numbers as $number) {
            $res = bcdiv($res, (string)$number, $scale);
        }
        return $res;
    }

    /**
     * 乘
     * @param array $numbers
     * @param int $scale
     * @return string
     */
    public static function mul(array $numbers, int $scale): string
    {
        $res = sprintf("%.2f",$numbers[0]);
        unset($numbers[0]);
        foreach ($numbers as $number) {
            $res = bcmul($res, (string)$number, $scale);
        }
        return $res;
    }

    

}